package m.jclecai.com.fragment.follow;

import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.Response.ResFollowOrederPeopleList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/11/18.
 * 跟单用户的Fragment;
 */

public class HotOrderPeopleFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2 {

    public String mFollowOrderId = "";

    @BindView(R.id.tv_title)
    TextView mTvTitle;

    @BindView(R.id.ll_item)
    LinearLayout mLlItem;

    @BindView(R.id.ptr_listView)
    PullToRefreshListView mPtrListView;

    @BindView(R.id.fl_content)
    LinearLayout mFlContent;

    int mPage = 1;
    @BindView(R.id.fl_empty)
    LinearLayout mFlEmpty;


//    private OrderFollowPeopleAdapter mHotPeopleListAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot_order_people;
    }


    @Override
    public void initData() {
        super.initData();
//        mHotPeopleListAdapter = new OrderFollowPeopleAdapter(getActivity());
//        mPtrListView.setAdapter(mHotPeopleListAdapter);
        mPtrListView.setMode(PullToRefreshBase.Mode.BOTH);
        mPtrListView.setOnRefreshListener(this);
    }


    public void requestData() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("id", mFollowOrderId + "");    //订单id;
        paramsMap.put("page", mPage + "");
        //这里我们是需要订单id
        HttpManager.postAsyn(Constant_Url.FOLLOW_ORDER_PEOPLE_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                ToastUtil.showToast(getActivity(), "网络连接异常!!!");
                mPtrListView.onRefreshComplete();
            }

            @Override
            public void onResponse(String response) {
                if (!TextUtils.isEmpty(response)) {

                    ResFollowOrederPeopleList resHotPeople = FastOk.get(response, ResFollowOrederPeopleList.class);
                    if(resHotPeople!=null){
                        if ("0".equals(resHotPeople.getStatus())) {

//                            mHotPeopleListAdapter.addAll(resHotPeople.getRecords());

                            int totalPages = Integer.parseInt(resHotPeople.getPages());
                            if (mPage < totalPages) {
                                mPage += 1;
                            }

                        } else {
                            DisPlay(resHotPeople.getMessage());
                        }
                    }else{
                        DisPlay("网络异常!!!");
                    }
                } else {
                    DisPlay("网络异常!!!");
                }
            }
        }, paramsMap);
    }

//
//    /**
//     * 更新页面
//     *
//     * @param hotOrederDetail 这几串几，我们是不知道的所以这里我们是需要这样写的；
//     */
//    public void updateUI(ResHotOrederDetail hotOrederDetail, String orderId) {
//
//        mFollowOrderId = orderId;
//
//        String html = "当前跟单<html><font color='red'>" + hotOrederDetail.getFollow_count() + "</font></html>人 , " +
//                "跟单金额<html><nt color='red'>" + hotOrederDetail.getFollow_money() + "</font></html>元";
//
//
//        mTvTitle.setText(Html.fromHtml(html));
//
//        if (Integer.parseInt(hotOrederDetail.getFollow_count()) > 1) {
//            mFlContent.setVisibility(View.VISIBLE);
//            mFlEmpty.setVisibility(View.INVISIBLE);
//            requestData();
//        } else {
//            //这里表示没有跟单的列表
//            mFlContent.setVisibility(View.INVISIBLE);
//            mFlEmpty.setVisibility(View.VISIBLE);
//        }
//    }
//
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        mPage = 1;
//        mHotPeopleListAdapter.clear();
        requestData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        requestData();
    }
//
//
//    public class OrderFollowPeopleAdapter extends DefaultViewHolderAdapter<ResFollowOrederPeopleList.FollowPeopleBean> {
//
//
//        public OrderFollowPeopleAdapter(Context context) {
//            super(context);
//        }
//
//        @Override
//        public DefaultViewHolder<ResFollowOrederPeopleList.FollowPeopleBean> getRealViewHolder(Context context) {
//            return new PeopleViewHolder(context, R.layout.item_fragment_follow_people);
//        }
//    }
//
//    public class PeopleViewHolder extends DefaultViewHolder<ResFollowOrederPeopleList.FollowPeopleBean> {
//
//        @BindView(R.id.tv_username)
//        TextView mTvUsername;
//
//        @BindView(R.id.tv_one_money)
//        TextView mTvOneMoney;
//
//        @BindView(R.id.tv_add_time)
//        TextView mTvAddTime;
//
//        @BindView(R.id.ll_item)
//        LinearLayout mLlItem;
//
//        public PeopleViewHolder(Context context, @LayoutRes int resource) {
//            super(context, resource);
//        }
//
//        @Override
//        public void init(ResFollowOrederPeopleList.FollowPeopleBean followPeopleBean, int position) {
//
//
//            mTvUsername.setText(followPeopleBean.getUsername());
//            mTvOneMoney.setText(followPeopleBean.getOne_money());
//
//
//            String date = "";
//            try {
//                Date parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(followPeopleBean.getAddtime());
//                date = new SimpleDateFormat("MM-dd hh:mm").format(parse);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            if (TextUtils.isEmpty(date)) {
//                date = followPeopleBean.getAddtime();
//            }
//
//            mTvAddTime.setText(date);
//
//            if (position % 2 == 0) {
//                mLlItem.setBackground(new ColorDrawable(Color.WHITE));
//            } else {
//                mLlItem.setBackground(new ColorDrawable(getResources().getColor(R.color.color_f5f5f5)));
//            }
//
//        }
//    }

}
